/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.bo.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import unilearn.bo.SubmissionManagement;
import unilearn.util.web.Download;
import unilearn.vo.Role;
import unilearn.vo.Submission;

/**
 *
 * @author Ming
 */
public class SubmissionDownloadServlet extends HttpServlet {

    private SubmissionManagement submMgt;

    public void init() throws ServletException {
        WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
        submMgt = (SubmissionManagement) context.getBean("submMgt");
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = null;

        try {
            String submId = (String) request.getParameter("submId");
            if ((submId == null) || (submId.equals(""))) {
                throw new NullPointerException("Could not find submission id.");
            }
//            System.out.println("[SubmissionDownloadServlet]submId: " + submId);//***

            Submission subm = submMgt.getSubmission(new Integer(submId));
            if (subm == null) {
                throw new NullPointerException("Could not find submission.");
            }
//            System.out.println("[SubmissionDownloadServlet]subm: " + subm.toString());//***

            byte[] fileBytes = submMgt.getAssignmentFile(subm);
            String fileName = subm.getFileUrl();
//            System.out.println("[SubmissionDownloadServlet][0]fileName: " + fileName);//***
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
//            System.out.println("[SubmissionDownloadServlet][1]fileName: " + fileName);//***

            Download.doDownload(response, fileName, fileBytes);
        } catch (Exception e) {
            page = null;
            System.out.println(getClass().getName() + "[processRequest]\n" + e.toString());
        } finally {
            if (page == null) {
                response.sendRedirect(request.getHeader("referer"));
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
